package com.vegetable.modules.service.bank.convert;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.vegetable.modules.entity.finance.Transactions;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 中行流水标准化字段输出
 */
@Slf4j
public class BOCConverter extends BankGenerator{
    private static final String bankMark = "BOCC";
    public List<Transactions> convert(MultipartFile file) throws Exception {
        List<com.vegetable.modules.entity.finance.Transactions> result  = convertA(file);
        if(CollectionUtil.isEmpty(result)){
            result = convertB(file);
        }
        return result;
    }

    private List<Transactions> convertA(MultipartFile file) {
        List<Transactions> result = new ArrayList<>();
        try (Workbook workbook = WorkbookFactory.create(file.getInputStream())) {
            Sheet sheet = workbook.getSheetAt(0);
            int lastRow = sheet.getLastRowNum();
            for(int i = 9; i <= lastRow; i++){
                Row currentR = sheet.getRow(i);
                Transactions thisT = new Transactions();
                thisT.setTradingType("往账".equals(getStringCell(currentR,0)) ? "EXPENSE" : "INCOME");
                // 1. 解析日期部分 (yyyyMMdd)
                LocalDate date = LocalDate.parse(getStringCell(currentR,10), DateTimeFormatter.BASIC_ISO_DATE);
                // 2. 解析时间部分 (HH:mm:ss)
                LocalTime time = LocalTime.parse(getStringCell(currentR,11));
                LocalDateTime dateTime = LocalDateTime.of(date, time);
                Date legacyDate = Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
                thisT.setTradingTime(legacyDate);
                thisT.setTradingAmount(getBCCell(currentR,13).abs());
                thisT.setBalanceAmount(getBCCell(currentR,14));
                thisT.setBankName("INCOME".equals(thisT.getTradingType()) ? getStringCell(currentR,3) : getStringCell(currentR,7));
                thisT.setBankNo("INCOME".equals(thisT.getTradingType()) ? getStringCell(currentR,4) : getStringCell(currentR,8));
                thisT.setCounterparty("INCOME".equals(thisT.getTradingType()) ? getStringCell(currentR,5) : getStringCell(currentR,9));
                thisT.setTradingDescription(getStringCell(currentR,23));
                thisT.setRemark(getStringCell(currentR,25));
                thisT.setChannel(bankMark);
                thisT.setTradingNo(Transactions.generateUniqueFlowId(thisT));
                result.add(thisT);
            }
        }catch (Exception e){
            log.error("转换中行流水发生异常--->", e);
            return result;
        }
        return result;
    }

    private List<Transactions> convertB(MultipartFile file) {
        List<Transactions> result = new ArrayList<>();
        try (Workbook workbook = WorkbookFactory.create(file.getInputStream())) {
            Sheet sheet = workbook.getSheetAt(0);
            int lastRow = sheet.getLastRowNum();
            for(int i = 1; i <= lastRow; i++){
                Row currentR = sheet.getRow(i);
                Transactions thisT = new Transactions();
                thisT.setTradingType(getDoubleCell(currentR, 3) > 0 ? "EXPENSE" : "INCOME");
                thisT.setTradingTime(DateUtil.parse(getStringCell(currentR, 2), "yyyyMMdd HH:mm:ss"));
                thisT.setTradingAmount("INCOME".equals(thisT.getTradingType()) ? getBCCell(currentR, 4) : getBCCell(currentR, 3));
                thisT.setBalanceAmount(getBCCell(currentR,5));
                thisT.setBankName(getStringCell(currentR,9));
                thisT.setBankNo(getStringCell(currentR,8));
                thisT.setCounterparty(getStringCell(currentR,7));
                thisT.setTradingDescription(getStringCell(currentR,11));
                thisT.setRemark(getStringCell(currentR,12));
                thisT.setChannel(bankMark);
                thisT.setTradingNo(Transactions.generateUniqueFlowId(thisT));
                result.add(thisT);
            }
        }catch (Exception e){
            log.error("转换中行流水发生异常--->", e);
            return result;
        }
        return result;
    }

}
